<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Mail;

class Adminlist extends Controller
{
    public function showlist(){
        $data = User::orderby('id','desc')->paginate(10);
        return view('admin.user.index',['data'=>$data]);
    }

    public function showAdd(){
        return view('admin.user.showAdd');
    }

    public function add(Request $request){
//        $data = $request->post();

        $params = $this->validate($request, [
            'username' => 'required',
            'truename' => 'required',
            'password' => 'required',
            'email' => 'required',
            'phone' => 'required'
        ]);

        Mail::raw('李丽丽丽丽丽丽',function (Message $message) use ($params){
            $message->to($params['email']);
            $message->subject('李安康');
        });

        Mail::send('admin.user.email',$params,function (Message $message) use ($params){
            $message->to($params['email']);
            $message->subject('测试');
        });

        $data['password'] = bcrypt($params['password']);
        $res = User::query()->insert($params);
        if ($res){
            return redirect('showlist');
        }else{
            return redirect('showAdd')->withErrors('添加失败');
        }
    }

    public function del($id){
        User::destroy($id);
        return redirect('showlist');
    }

    public function delAll(Request $request){
        $id = $request->post('id');
       $res =  User::destroy($id);
       if ($res){
           return ['code'=>200,'msg'=>'成功','data'=>''];
       }else{
           return ['code'=>2001,'msg'=>'失败','data'=>''];
       }

    }


    public function restore(){
        $data = User::onlyTrashed()->paginate(10);
        return view('admin.user.restore',['data'=>$data]);
    }

    public function foreDelete($id){
        User::query()->where('id',$id)->forceDelete();
        return redirect('restore');
    }

    public function ten($id){
        User::where('id', $id)->restore();
        return redirect('restore');
    }




}
